package com.boolib.animate.commons.utils;


import org.apache.commons.fileupload.FileUploadBase;
import org.apache.commons.fileupload.FileUploadException;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

/**
 * 功能描述：
 *
 * @author NIER
 * @description
 * @since 2019/9/10 13:32
 */


public class UploadUtil {


	public static String  getBaseUri(String x ){

		return x.substring(x.lastIndexOf("/")+1);
	}
	private static String getExt(String ext ){
		return  ext.substring(ext.lastIndexOf("."));
	}

	private static String reName(String x ){
		 return UUID.randomUUID().toString().replaceAll("-","")+getExt(x);
	}

	public static  String transferTo(String filePath, MultipartFile multipartFile) throws IOException, FileUploadException {

		ImageFileUtil.isImage(multipartFile);

		String image = reName( multipartFile.getOriginalFilename());


		File file = new File(filePath);

		if(!file.exists()){
			file.mkdirs();
		}

		String uri = filePath+File.separator+image;

		multipartFile.transferTo(new File(uri));

		return image;


	}

	public static void remove( String filepath,String target){

		StringBuilder stringBuilder = new StringBuilder();

		String basePathx  = stringBuilder.append(filepath).append(File.separator).append(target).toString();

		File file = new File(basePathx);

		if(file.exists()){

			file.delete();
		}

	}

}
